package com.huatek.framework.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class CutPageTag extends BodyTagSupport{

	String formName;
	boolean isExistForm;
	StringBuffer tag =null;
	String pageId;
	public StringBuffer getTag() {
		return tag;
	}
	public boolean getIsExistForm() {
		return isExistForm;
	}
	public void setIsExistForm(boolean isExistForm) {
		this.isExistForm = isExistForm;
	}
	String actionUrl;
	String offerPageSize;
	boolean isOuterForm;
	public boolean getIsOuterForm() {
		return isOuterForm;
	}
	public void setIsOuterForm(boolean isOuterForm) {
		this.isOuterForm = isOuterForm;
	}
	public String getOfferPageSize() {
		return offerPageSize;
	}
	public void setOfferPageSize(String offerPageSize) {
		this.offerPageSize = offerPageSize;
	}
	int pageNum = 6;
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public String getActionUrl() {
		return actionUrl;
	}
	public void setActionUrl(String actionUrl) {
		this.actionUrl = actionUrl;
	}
	CutPageBean pageBean;

	public String getFormName() {
		return formName;
	}
	public void setFormName(String formName) {
		this.formName = formName;
	}
	public CutPageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(CutPageBean page) {
		this.pageBean = page;
	}
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	 public int doStartTag() throws JspException {
		 tag = new StringBuffer();
		 if(formName==null){
			 formName = "defaultQueryForm";
		 }
		 if(!isExistForm){
			 tag.append("<form id=\""+formName+"\" name=\""+formName+"\" action=\""+actionUrl+"\" method=\"post\"><div class=\"page\">");
		 }
		 if(!isOuterForm){
			 tag.append("<input type=\"hidden\" name=\"currentPage\" value=\""+pageBean.getCurrentPage()+"\"/>");
			 tag.append("<input type=\"hidden\" name=\"pageSize\" value=\""+pageBean.getPageSize()+"\"/>");
		 }
		 tag.append("<div class=\"pagedisplay\">Page "+pageBean.getCurrentPage()+" of "+pageBean.getTotalPage()+"</div>");
		 tag.append("<div class=\"pageinformation\">");
		 tag.append(displayPages());
		 tag.append("</div>");
		 String submitString = getSubmitString(pageBean.getCurrentPage());
		 if(offerPageSize!=null){
			 tag.append("<select class=\"pageselectstyle\" name=\"choosePageSize\" onchange=\"document.forms."+this.formName+".pageSize.value=this.value;"+submitString+"\">");
			 String[] pageSizes = offerPageSize.split(",");
			 for(int i=0;i<pageSizes.length;i++){
				 if(pageSizes[i].trim().length()==0){
					 continue;
				 }
				tag.append("<option value=\""+pageSizes[i]+"\""+(pageSizes[i].equals(pageBean.getPageSize()+"")?" selected":"")+" >"+pageSizes[i]+"</option>");
			 }
			 tag.append("</select>");
		 }
		 if(!isExistForm){
			 tag.append("</div></form>");
		 }
		 try {
	            JspWriter out = pageContext.getOut();
	            out.print(tag.toString());
	        } catch (IOException ex) {
	            throw new JspException(ex);
	        }
		 return EVAL_BODY_INCLUDE;
	 }

	 public String displayPages(){
			StringBuffer displayString = new StringBuffer();

			if(pageBean.getCurrentPage() - pageNum >0){
				displayString.append(" <a class=\"previous\" href=\"javascript:"+getSubmitString(pageBean.getCurrentPage() >pageNum?(((pageBean.getCurrentPage()-1)/pageNum-1)*pageNum+pageNum):1)+"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a> ");
			}
			if(pageNum>pageBean.getCurrentPage()){
				for(int  i=1;i<pageNum+1&&i<pageBean.getTotalPage()+1;i++){
					if(i!=pageBean.getCurrentPage()){
						displayString.append(" <a class=\"pagespacing\" href=\"javascript:"+getSubmitString(i)+"\"> "+i+" </a> ");
					}else{
						displayString.append("<font class=\"currentpagespacing\"> "+i+" </font>");
					}
				}
				if(pageBean.getTotalPage()>pageNum){
					displayString.append(" <a class=\"next\" href=\"javascript:"+getSubmitString(((pageBean.getCurrentPage()-1)/pageNum+1)*pageNum+1)+"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a> ");
				}
			}else{
				//if(this.currentPage%pageNum==0){this.currentPage=this.currentPage-1;}
				for(int i=((pageBean.getCurrentPage()-1)/pageNum)*pageNum+1;i<((pageBean.getCurrentPage()-1)/pageNum)*pageNum+pageNum+1&&i<pageBean.getTotalPage()+1;i++){
					if(i!=pageBean.getCurrentPage()){
						displayString.append(" <a class=\"pagespacing\" href=\"javascript:"+getSubmitString(i)+"\"> "+i+" </a> ");
					}else{
						displayString.append("<font class=\"currentpagespacing\"> "+i+" </font>");
					}
				}
				if(pageBean.getTotalPage()>((pageBean.getCurrentPage()-1)/pageNum+1)*pageNum){
					displayString.append(" <a class=\"next\" href=\"javascript:"+getSubmitString(((pageBean.getCurrentPage()-1)/pageNum+1)*pageNum+1)+"\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a> ");
				}
			}
			return displayString.toString();
		}
	 public String getSubmitString(int currentPage){
		 String submitString = "document.forms."+this.formName+".currentPage.value='"+currentPage+"';document.forms."+this.formName+".submit();";
		 if(isOuterForm){
			 submitString = "getFilterPageVariable(document.forms."+this.formName+","+pageBean.getPageSize()+","+currentPage+");document.forms."+this.formName+".submit();";
		 }
		 return submitString;
	 }
	public String getPageId() {
		return pageId;
	}
	public void setPageId(String pageId) {
		this.pageId = pageId;
	}

}
